package com.usefullc.crawler.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.exception.BizException;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.crawler.domain.Active;
import com.usefullc.crawler.domain.Template;
import com.usefullc.crawler.mapper.TemplateMapper;
import com.usefullc.crawler.query.ActiveQuery;
import com.usefullc.crawler.query.TemplateQuery;
import com.usefullc.crawler.vo.ActiveVo;
import com.usefullc.crawler.vo.TemplateVo;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.service.AbstractBizService;
import com.usefullc.system.service.SerialNumberService;
import com.usefullc.system.vo.TreeVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 爬虫模板服务类
 * @author: 星空
 * @date: 2022-07-26
 */
@Service
public class TemplateService extends AbstractBizService<TemplateMapper, Template, TemplateQuery> {


    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private ActiveService activeService;

    @Override
    protected QueryWrapper<Template> getQueryWrapper(TemplateQuery query) {
        QueryWrapper<Template> queryWrapper = super.getQueryWrapper(query);
        return queryWrapper;
    }

    public Pager<TemplateVo> findVoByPage(TemplateQuery query){
        Pager<Template> page = this.findByPage(query);
        Pager<TemplateVo> newPage = new Pager<>(page);
        List<Template> dataList = page.getDataList();
        List<TemplateVo> newDataList = new ArrayList<>();
        newPage.setDataList(newDataList);
        for (Template template : dataList) {
            TemplateVo templateVo = BeanUtils.beanCopy(template, TemplateVo.class);
            //处理子集
//            ActiveQuery activeQuery = new ActiveQuery();
//            List<ActiveVo> activeList = activeService.findVoAll(activeQuery);
//            templateVo.setChildren(activeList);
            newDataList.add(templateVo);
        }
        return newPage;
    }

    public List<TemplateVo> findVoAll(TemplateQuery query){
        List<Template> dataList = this.findAll(query);
        List<TemplateVo> newDataList = new ArrayList<>();
        for (Template template : dataList) {
            TemplateVo templateVo = BeanUtils.beanCopy(template, TemplateVo.class);
            //处理子集
//            ActiveQuery activeQuery = new ActiveQuery();
//            List<ActiveVo> activeList = activeService.findVoAll(activeQuery);
//            templateVo.setChildren(activeList);
            newDataList.add(templateVo);
        }
        return newDataList;

    }



    public Pager<TreeVo> findTreeVoByPage(TemplateQuery query){
        Pager<Template> page = this.findByPage(query);
        Pager<TreeVo> newPage = new Pager<>(page);
        List<Template> dataList = page.getDataList();
        List<TreeVo> newDataList = new ArrayList<>();
        newPage.setDataList(newDataList);
        for (Template template : dataList) {
            TreeVo treeVo = new TreeVo();
            newDataList.add(treeVo);
            treeVo.setId(template.getId());
            treeVo.setCode("template"+template.getId());
            treeVo.setTitle(template.getName());
            treeVo.setText(template.getDescription());
            //处理子集
            ActiveQuery activeQuery = new ActiveQuery();
            activeQuery.setTemplateId(template.getId());
            activeQuery.setOrderByColumn("orderNo");
            activeQuery.setAscOrDesc("asc");
            List<Active> activeList = activeService.findAll(activeQuery);
            List<TreeVo> children = new ArrayList<>();
            treeVo.setChildren(children);
            for (Active active : activeList) {
                treeVo = new TreeVo();
                treeVo.setId(active.getId());
                treeVo.setCode("active"+active.getId());
                treeVo.setTitle(active.getName()+"_"+active.getOrderNo());
                children.add(treeVo);
            }
        }
        return newPage;
    }


    @Transactional
    public void copy(Long id){
        Template template = this.findById(id);
        template.setId(null);
        template.setName(template.getName()+"_copy");
        this.save(template);

        //获取子集
        ActiveQuery activeQuery = new ActiveQuery();
        activeQuery.setTemplateId(id);
        List<Active> activeList = activeService.findAll(activeQuery);
        for (Active active : activeList) {
            active.setTemplateId(template.getId());
            active.setId(null);
        }
        activeService.saveEntityList(activeList);
    }


    public int updateStatus(Long id,Integer status){
        return getBaseMapper().updateStatus(id, status);
    }






}
